
let App = getApp();
var that;

Page({
	data: {
		delBtnWidth: 180,
		visual: 'hidden',
		addressList:[],
		isSwitchAddress:false,
		coupon_code:'',
		coupon_id:''
	},
	//手指触摸动作开始 记录起点X坐标
	  touchstart: function(e) {
	    //开始触摸时 重置所有删除
	    let data = App.touch._touchstart(e, this.data.addressList)
	    this.setData({
	      addressList: data
	    })
	  },
	 
	  //滑动事件处理
	  touchmove: function(e) {
	    let data = App.touch._touchmove(e, this.data.addressList)
	    this.setData({
	      addressList: data
	    })
	  },
	  
	 
	  //删除事件
	  del: function(e) {
		  
	    wx.showModal({
	      title: '提示',
	      content: '确认要删除此条信息么？',
	      success: function(res) {
	        if (res.confirm) {
	          console.log('用户点击确定')
			  wx.request({
			  	url: App.url.requestUrl + 'Adress/del',
			  	method: 'POST',
			  	 header: {'content-type': 'application/x-www-form-urlencoded',"token":wx.getStorageSync('access_token')},
			  	dataType:'json',
				data:{
					address_id:e.currentTarget.dataset.id
				},
			  	success: function (res) {
			  		console.log(res.data)
			  	  if(res.data.ret == 200){
			  	 wx.showToast({
			  	   title: res.data.msg,
			  	   duration: 2000,
			  	   mask: true,
			  	   icon: 'none',
			  	 
			  	 })
			  			 
			  		 }
			  	}
			  })
			  setTimeout(function(){
			     that.addressList()
			  },2000)
			  
	         
	         
	        } else if (res.cancel) {
	          console.log('用户点击取消')
	        }
	      }
	    })
	  },
	addressList(){
		that = this;
		console.log(App.url.requestUrl)
		wx.request({
			url: App.url.requestUrl + 'Adress/lists',
			method: 'POST',
			 header: {'content-type': 'application/x-www-form-urlencoded',"token":wx.getStorageSync('access_token')},
			dataType:'json',
			success: function (res) {
				console.log(res.data)
			  if(res.data.ret == 200){
          console.log(res.data.data.length)
				  if(res.data.data.length){
					  that.setData({
					    addressList:res.data.data
					  })
				  }else{
					  that.setData({
					     visual: 'show',
					  })
					 
				  }
			  console.log(res.data)
					  
				 }
			}
		})
	},
	onLoad: function (options) {
		console.log(options)
		that = this;
		that.addressList()
		if (options.isSwitchAddress) {
			that.setData({
				isSwitchAddress: true,
				coupon_code:options.coupon_code,
				coupon_id:options.coupon_id
				
			});
		}
	},
	getid(e){
		that = this;
		var id = e.currentTarget.dataset.id;
    console.log(that.data.isSwitchAddress)
		if(that.data.isSwitchAddress == true){
			console.log(0)
			wx.navigateTo({
				url: '../../order/checkout/checkout?id='+id+'&coupon_code='+that.data.coupon_code+'&coupon_id='+that.data.coupon_id
			});
		}
	},
	onShow: function (option) {
    console.log(option)
    if (this.data.isSwitchAddress==true){

    }
		// that.getAddress();	
	},
	add: function () {
		wx.navigateTo({
      url: '../add/add?isSwitchAddress=' + this.data.isSwitchAddress + '&coupon_code=' + that.data.coupon_code + '&coupon_id=' + that.data.coupon_id
		});
	},
	
	
	edit: function (e) {
		var id = e.currentTarget.dataset.id;
		var name = e.currentTarget.dataset.name;
		var phone = e.currentTarget.dataset.phone;
		var province_name = e.currentTarget.dataset.province_name;
		var city_name = e.currentTarget.dataset.city_name;
		var region = e.currentTarget.dataset.region;
		var text = e.currentTarget.dataset.text;
		
		console.log(id)
		wx.navigateTo({
			// url: '../add/add?objectId=' + id 
			url: '../add/add?name='+name+'&phone='+phone+'&province_name='+province_name+'&city_name='+city_name+'&region='+region+'&text='+text+'&objectId=' + id 
		})
	},
	selectAddress: function (e) {
		if (!that.data.isSwitchAddress) {
			return;
		}
		var index = e.currentTarget.dataset.index;
		WxNotificationCenter.postNotificationName("addressSelectedNotification", that.data.addressList[index].id);
		wx.navigateBack();
	}
})